/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//jazz_bgmusic   - http://www.partnersinrhyme.com/
//right_sound	 - http://www.soundjay.com/
//wrong_sound	 - http://www.soundjay.com/

package com.Arkham;

import java.util.Scanner;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MathGame extends Activity implements OnClickListener
{

	Scanner in = new Scanner(System.in);
	//This must be passed parameters that set the following variables
	boolean add 	= true;		//default for testing, this should be passed in
	boolean sub 	= true;		//default for testing, this should be passed in
	boolean mult 	= true;		//default for testing, this should be passed in
	boolean div 	= true;		//default for testing, this should be passed in
	boolean par 	= false;	//default for testing, this should be passed in
	int max_Val 	= 10;		//default for testing, this should be passed in
	int total_Num 	= 4;		//default for testing, this should be passed in
	String equation ="";
	private boolean ans = false;  //did user answer question correctly?
	private Long startTime, endTime, time;  //time to answer question

	private Intent intent;
	private Bundle bundle;

	MathProblem problem = new MathProblem(total_Num, max_Val, add, sub, mult, div, par);
	TextView Equation, Answer;											//Text 
	MediaPlayer bgMusic, wrongSound, rightSound;						//Sound


	protected void onCreate(Bundle savedInstanceState) {

		this.requestWindowFeature(Window.FEATURE_NO_TITLE); 			//Removes title bar and sets the theme

		super.onCreate(savedInstanceState);
		setLayout();													//Set layout

//		bgMusic = MediaPlayer.create(this, R.raw.jazz_bgmusic);			//Set background music
//		bgMusic.setLooping(true);										//Set bgMusic to loop
//		bgMusic.start();												//Plays the music
		rightSound = MediaPlayer.create(this, R.raw.right_sound);		//Set sound effect for correct entry
		wrongSound = MediaPlayer.create(this, R.raw.wrong_sound);		//Set sound effect for incorrect entry
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) { 		//Handle orientation change
		super.onConfigurationChanged(newConfig); 						
		setLayout();													//Set layout when orientation changes
	}

	@Override
	protected void onPause()											//When the user hits the back button
	{
		super.onPause();
//		bgMusic.stop();													//Stop background music
		super.finish();													//Kill this view
	}


	public void onClick(View v) {										//Handles button clicks

		String answer, sol = "";										//Initialize strings
		sol = problem.simplifyEquation();							//Solve equation
		answer = Answer.getText().toString().replace(" ", "");			//Get rid of spaces

		if (sol.equals(answer))										//Compare user's answer and solution
		{
			if(rightSound != null)
				rightSound.start();										//Play sound effect for correct answer
			ans=true;
		}
		else
		{
			if(wrongSound != null)
				wrongSound.start();									//Play sound effect for incorrect answer
			ans=false;
		}

		endTime = System.currentTimeMillis();
		time = endTime - startTime;
		intent = new Intent();
		bundle = new Bundle();
		bundle.putBoolean("ANS", ans);
		bundle.putLong("TIME", time);
		intent.putExtras(bundle);
		setResult(Activity.RESULT_OK, intent);
		finish();

	}


	public void setLayout()
	{
		String sol="";
		setContentView(R.layout.math);									//Sets up the math.xml file
		Answer = (EditText) findViewById(R.id.answerInput);				//Textbox for answering, only accepts numbers
		Equation = (TextView) findViewById(R.id.equation);				//Label with the equation printed to it
		
		do
		{
		problem.NewGame();
		equation = problem.getEquation();								//Get equation
		
		sol = problem.simplifyEquation();
		}while(Integer.parseInt(sol)<0);
		Equation.setText(equation);										//Prints new equation in label

		View EnterButton = findViewById(com.Arkham.R.id.enterButton);	//Sets up the enter button
		EnterButton.setOnClickListener(this);  							//Listens for enter to be hit
		startTime = System.currentTimeMillis();//this may allow people to cheat by tilting phone
	}
}
